package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import clientes.Cliente;
import delegaciones.Delegacion;

public class JDBCClienteDAO implements IClienteDAO{
	
	private Connection conn;
	
	public JDBCClienteDAO(){
		
		conn = ConnectionManager.getInstance().checkOut();
		
		
	}

	@Override
	public void delete(String dni) {
		String sql = "DELETE FROM clientes WHERE (dni = ?) ";
        PreparedStatement stmt = null;
		
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dni);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}

	@Override
	public void insert(Cliente c) {
		String sql = "INSERT INTO clientes (dni, nombre, direccion, telefono, provdelegacion, cifempresa) VALUES (?, ?, ?, ?, ?, ?) ";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, c.getDNI());
            stmt.setString(2, c.getNombre());
            stmt.setString(3, c.getDireccion());
            stmt.setString(4, c.getTelefono());
            stmt.setString(5, c.getDelegacion().getProvincia());
            stmt.setString(6, c.getDelegacion().getCIF());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}

	@Override
	public void modificacliente(Cliente c) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "UPDATE clientes SET nombre = ?, direccion = ?, telefono = ?, provdelegacion = ?, cifempresa = ?" +
				"WHERE (dni = ?)";
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, c.getNombre());
			stmt.setString(2, c.getDireccion());
            stmt.setString(3, c.getTelefono());
            stmt.setString(4, c.getDelegacion().getProvincia());
            stmt.setString(5, c.getDelegacion().getCIF());            
            stmt.setString(6, c.getDNI());
			
			stmt.executeUpdate();
		}catch(SQLException ex){
			System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
		
	}

	@Override
	public Cliente select(String dni) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Cliente c = null;
        Delegacion d = null;
       
        String sql = "SELECT * FROM clientes WHERE (dni = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, dni);
            result = stmt.executeQuery();

            if(result.next()){
            	c = new Cliente();
            	d = new Delegacion();
            	c.setDNI(result.getString("dni"));
            	c.setNombre(result.getString("nombre"));
            	c.setDireccion(result.getString("direccion"));
            	c.setTelefono(result.getString("telefono"));
            	d.setProvincia(result.getString("provdelegacion"));
        		d.setCIF(result.getString("cifempresa"));
        		c.setDelegacion(d);

            }
        } catch (SQLException u) {
            System.out.println("Message: " + u.getMessage());
            System.out.println("SQLState: " + u.getSQLState());
            System.out.println("ErrorCode: " + u.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException u) {
            }
        }
        return c;
	}
	

	@Override
	public List<Cliente> selectclientesdelegacion(Delegacion d) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<Cliente> listaclientes = new LinkedList<Cliente>();
        String sql = "SELECT * FROM clientes WHERE (provdelegacion = ? AND cifempresa = ?) ORDER BY dni";
    
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, d.getProvincia());
            stmt.setString(2, d.getCIF());
            result = stmt.executeQuery();

            while (result.next()) {
                Cliente c = new Cliente();
                
                c.setDNI(result.getString("dni"));
                c.setNombre(result.getString("nombre"));
                c.setDireccion(result.getString("direccion"));
                c.setTelefono(result.getString("telefono"));
                
                listaclientes.add(c);
            }
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
        return listaclientes;
	}	

	@Override
	public List<Cliente> selecttodosclientes() {
		PreparedStatement stmt = null;
        ResultSet result = null;
        List<Cliente> listaclientes = new LinkedList<Cliente>();
        String sql = "SELECT * FROM clientes ORDER BY nombre";
    
        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();

            while (result.next()) {
                Cliente c = new Cliente();
                
                c.setDNI(result.getString("CIF_DNI"));
                c.setNombre(result.getString("nombre"));
                c.setDireccion(result.getString("direccion"));
                c.setTelefono(result.getString("telefono"));
                
                listaclientes.add(c);
            }
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
        return listaclientes;
	}
	
	
	public void finalize(){
		ConnectionManager.getInstance().checkIn(conn);
		
	}

}
